8222. The length of the substring

 

Let’s start the numbering of characters in the string from 1. The substring S[i..j] is a sequence of characters that begins in position ³ and ends in position j. For given indexes ³ and j print the length of substring and all the characters of this substring not violating their order in the string.

 

Input. First line contains string S, which length is no more than 100. Second line contains two indexes i and j (1 ≤ ³j ≤ size(S), where size(S) is the length of the string).

 

Output. In the first line print the length of the substring. In the second line print all characters of substring in the same order like in the string S.

 

Sample input

Sample output

abracadabra

2 5

4

brac

 

 

SOLUTION

string

 

Algorithm analysis

Read the input string into a character array, starting from the first (not zero) position. The length of the substring S[i..j] is equal to ji + 1. Print the substring characters using the for loop.

 

Read the input string into a variable of type string. Let’s add a space character in front of it so that the numbering of letters starts from 1. Use the substr method to find a substring.

 

Algorithm realization

Declare a character array to store the string.

 

char s[110];

 

Read the input data. Since the indexing in the string starts from 1, read the string into array starting from the first position.

 

gets(s + 1);

scanf("%d %d", &i, &j);

 

Print the length of the substring S[i..j], it is equal to ji + 1.

 

printf("%d\n", j - i + 1);

 

Print the characters of a substring.

 

for (k = i; k <= j; k++)

  printf("%c", s[k]);

printf("\n");

 

Algorithm realization – STL

Read the input data. Add a space to the input string s so that the numbering of input characters starts from 1.

 

cin >> s; s = " " + s;

cin >> i >> j;

 

Find a substring of length ji + 1, that starts at position i.

 

res = s.substr(i, j - i + 1);

 

Print the answer: the length of the required substring and the substring itself.

 

cout << res.length() << endl;

cout << res << endl;

 

Java realization

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    String s = con.nextLine();

    s = " " + s;

 

    int i = con.nextInt();

    int j = con.nextInt();

 

    System.out.println(j - i + 1);

    s = s.substring(i,j+1);

    System.out.println(s);

    con.close();

  }

}

 

Python realization

 

s = input()

a, b = map(int,input().split())

print (b-a+1)

print (s[a-1:b])